package softtech.apps.callblocker.widget;

import softtech.apps.callblocker.R;
import softtech.apps.callblocker.TextEnable;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.RemoteViews;
import android.widget.Toast;

public class ReceiveWidget extends BroadcastReceiver{

	public static int UseApp = 1;
	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		if (intent.getAction().equals("softtech.apps.callblocker.widget.UPDATE_WIDGET")) {
			updateWidgetPictureAndButtonListener(context);
		}
		if(intent.getAction().equals("callblocker.widget.ACTIVE")){
			if(TextEnable.getEnableApp(context)==1){
				UseApp =1;
			}
			else{
				UseApp=0;
			}
			updateWidgetPictureAndButtonListener(context);
		}
	}
	private void updateWidgetPictureAndButtonListener(Context context) {
		RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
				R.layout.widget_enable_disable);

		// updating view

		if(UseApp==1){
			
			remoteViews.setInt(R.id.ivWidget, "setBackgroundResource", R.drawable.widgetdisable);
			TextEnable.fTextCreate("1", context);
		}else{
			remoteViews.setInt(R.id.ivWidget, "setBackgroundResource", R.drawable.widgetenable);
			TextEnable.fTextCreate("0", context);
		}
		// re-registering for click listener
		remoteViews.setOnClickPendingIntent(R.id.ivWidget,
				ProviderWidget.buildButtonPendingIntent(context));

		ProviderWidget.pushWidgetUpdate(context.getApplicationContext(),
				remoteViews);
	}

}